﻿var page = {
    weahterInfoTimer: 0,//weather定时器

    //加载缓冲UI
    loadWaitingImg: function (name) {
        var data = new Object();
        data[name] = { empty: true };
        this.setData(data);
        var data = new Object();
        data[name] = { panel: { page: this, xml: "Panels/WaitSmallItem", data: {} } };
        this.setData(data);
        this.setData({ animatedWaiting: { id: name + "AnimatedImage", value: "start" } });//加载动图
    },

    /* 此方法在第一次显示窗体前发生 */
    onLoad: function (event) {
        var that = this;
        refreshDate(this);                                  //获取信号质量--app.js
        this.loadWaitingImg("weather_hourly_list");         //加载缓冲UI
        this.weahterInfoTimer = setInterval(function () {
            if (weatherInfo != 0) {                         //已经获取到数据
                clearInterval(that.weahterInfoTimer);
                that.loadHourlyWeatherList();               //加载未来5天 天气列表并展示UI
                that.loadWeatherInfo();                     //加载天气信息并展示UI
            }
        }, 1 * 1000)
    },

    //相关 page 关闭返回到当前 page 回调函数，并返回相关 page 中的参数
    onUpdate: function (event) {
        var that = this;

        //从城市列表返回，对不同城市进行天气请求
        if (event.value.page == "cityPage") {
            if (currentCity.name == event.value.name) {
                return;
            }
            currentCity.name = event.value.name;         //获取城市名
            currentCity.location = event.value.location; //获取城市位置

            func = function () {
                that.setData({ loaction: { value: currentCity.name } });
                that.loadWeatherInfo();         //加载天气信息并展示UI
                that.loadHourlyWeatherList();   //加载未来5天 天气列表并展示UI
            }
            getFeatureWeather(true, func);      //HTTP 请求
        }
    },

    loadWeatherInfo: function () {
        if (weatherInfo == 0) { //没有请求成功
            return;
        }

        this.setData({ currentTempeature_label: { value: weatherInfo.hourly.Temperature[0].value } });

        switch (weatherInfo.hourly.Skycon[0].value) {
            case "CLEAR_DAY":
            case "CLEAR_NIGHT":
                this.setData({ weather_img: { value: "clear_white.png" } })
                this.setData({ weather_label: { value: "晴" } })
                break;
            case "PARTLY_CLOUDY_DAY":
            case "PARTLY_CLOUDY_NIGHT":
                this.setData({ weather_img: { value: "cloudy_white.png" } })
                this.setData({ weather_label: { value: "多云" } })
                break;
            case "CLOUDY":
                this.setData({ weather_img: { value: "overcase_white.png" } })
                this.setData({ weather_label: { value: "阴" } })
                break;
            case "WIND":
                this.setData({ weather_img: { value: "windy_white.png" } })
                this.setData({ weather_label: { value: "大风" } })
                break;
            case "HAZE":
                this.setData({ weather_img: { value: "haze_white.png" } })
                this.setData({ weather_label: { value: "雾霾" } })
                break;
            case "RAIN":
                this.setData({ weather_img: { value: "rain_white.png" } })
                this.setData({ weather_label: { value: "雨" } })
                break;
            case "SNOW":
                this.setData({ weather_img: { value: "snow_white.png" } })
                this.setData({ weather_label: { value: "雪" } })
                break;
        }
        this.setData({ humidity_label: { value: parseInt(weatherInfo.hourly.Humidity[0].value * 100) + "%" } })

        this.setData({ weatherInfo_panel: { refresh: true } });
    },

    /*加载 weather_hourly_list --- 24小时内天气数据 -- 列表*/
    loadHourlyWeatherList: function () {
        var hourlyInfoList = new Array();
        this.setData({ weather_hourly_list: { empty: true } });
        for (var i = 0, len = weatherInfo.hourly.Skycon.length; i < len; i++) {
            var hourlyItem = new Object();
            hourlyItem.hourlyTime_label = new Object();
            if (i == 0) {
                hourlyItem.hourlyTime_label.value = "现在"
            } else {
                hourlyItem.hourlyTime_label.value = weatherInfo.hourly.Skycon[i].datetime.split(" ")[1].split(":")[0] + "时";
            }
            hourlyItem.imagebox1 = new Object();
            switch (weatherInfo.hourly.Skycon[i].value) {
                case "CLEAR_DAY":
                case "CLEAR_NIGHT":
                    hourlyItem.imagebox1.value = "clear_black.png";
                    break;
                case "PARTLY_CLOUDY_DAY":
                case "PARTLY_CLOUDY_NIGHT":
                    hourlyItem.imagebox1.value = "cloudy_black.png";
                    break;
                case "CLOUDY":
                    hourlyItem.imagebox1.value = "overcase_black.png";
                    break;
                case "WIND":
                    hourlyItem.imagebox1.value = "windy_black.png";
                    break;
                case "HAZE":
                    hourlyItem.imagebox1.value = "haze_black.png";
                    break;
                case "RAIN":
                    hourlyItem.imagebox1.value = "rain_black.png";
                    break;
                case "SNOW":
                    hourlyItem.imagebox1.value = "snow_black.png";
                    break;
            }
            hourlyItem.label1 = new Object();
            hourlyItem.label1.value = parseInt(weatherInfo.hourly.Temperature[i].value) + "℃";
            hourlyInfoList.push(hourlyItem);
        }
        this.setData({
            weather_hourly_list: {
                list: {
                    page: this, items: [{
                        xml: "Panels/WeatherHourlyItem", items: hourlyInfoList
                    }]
                }
            }
        });
    },

    //点击按钮语音播报
    onBroadcast: function (event) {
        var str = currentCity.name;
        console.log("weatherInfo======>" + weatherInfo.hourly.Skycon[0].value)
        switch (weatherInfo.hourly.Skycon[0].value) {
            case "CLEAR_DAY":
            case "CLEAR_NIGHT":
                str += "今天天气晴朗"
                break;
            case "PARTLY_CLOUDY_DAY":
            case "PARTLY_CLOUDY_NIGHT":
                str += "今天多云"
                break;
            case "CLOUDY":
                str += "今天阴天"
                break;
            case "WIND":
                str += "今天有风"
                break;
            case "HAZE":
                str += "今天有雾霾"
                break;
            case "RAIN":
                str += "今天下雨"
                break;
            case "SNOW":
        }
        str += (",气温为" + weatherInfo.hourly.Temperature[0].value + "度,相对湿度为百分之" + parseInt(weatherInfo.hourly.Humidity[0].value * 100) + "。");
        var that = this;
        var http = require('http');
        http.request({
            url: 'http://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=uGmHtBFMOkme2XeGWzKLREDv&client_secret=xG7I0fy4GQLPpLqanq7pZAGtNXGUW564',
            success: function (res) {
                var jnews = res.data.jsonParse();//获取access_token值
                var url = "http://tsn.baidu.com/text2audio?tex=" + str + "&lan=zh&cuid=44-b2-95-27-49-1f&ctp=1&per=4&tok=" + jnews.access_token;
                http.request({
                    url: url,
                    header: {
                        "Content-Type": "application/json"
                    },
                    success: function (res) {
                        console.log('request success');
                        var file = pm.getFileSystemManager();
                        file.writeFile({
                            filePath: "forecast.mp3",
                            data: res.data,
                            success: function () {
                                audio.stop();
                                audio.setSrc("/mnt/sd0/filesystem/forecast.mp3");
                                audio.play();
                            },
                            complete: function () { console.log("writeFile complete"); }
                        });
                    },
                })
            }
        })
    },

    /* 此方法展示窗体后发生 */
    onResume: function (event) {

    },

    /* 当前页状态变化为显示时触发 */
    onShow: function (event) {

    },

    /* 当前页状态变化为隐藏时触发 */
    onHide: function (event) {

    },

    /* 此方法关闭窗体前发生 */
    onExit: function (event) {
        if (this.weahterInfoTimer != 0) {
            clearInterval(this.weahterInfoTimer);
        }
        clearInterval(this.dateTimer);//app.js中定时器--刷新网络信号
    },

    onBtn: function (event) {
        pm.navigateBack();
    },

    onChooseCity: function (event) {
        console.log("@@event.touchs[0].type" + event.touchs[0].type);
        audio.stop();
        console.log("@@currentCity is:" + currentCity);
        pm.navigateTo({ url: "cityPage/cityPage", value: currentCity });
    },
};

Page(page);

page = 0;
